package com.zpf.controller;

import com.alibaba.fastjson.JSON;
import com.zpf.pojo.Type;
import com.zpf.service.TypeService;
import com.zpf.service.impl.TypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//商品分类
@WebServlet("/type")
public class TypeController extends BaseServlet{
    TypeService typeService = new TypeServiceImpl();
    public String findAllType(HttpServletRequest request, HttpServletResponse response){
        //1.调用service业务层返回集合
        List<Type> list = typeService.findAllType();
        //2.将数据放入request域中保存
        request.setAttribute("goodsTypeList",list);
        //3.请求转发
        return "forward:/admin/showGoodsType.jsp";
    }


    //商品添加下拉框需要的商品分类数据
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //1.调用service业务层返回集合
        List<Type> list = typeService.findAllType();
        //2.将商品分类集合对象数据，转换成json数据
        String jsonStr = JSON.toJSONString(list);
        //3.通过流将json格式字符串写回到原页面中
        response.getWriter().println(jsonStr);
    }
}
